package cn.sinjet.utils;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import cn.sinjet.entity.AppInfo;
import cn.sinjet.entity.RouteLine;
import cn.sinjet.entity.ShortPOI;

public class SharedPreferenceUtil {
	private SharedPreferences sp;
	private SharedPreferences.Editor editor;
    private static final String fileName = "hudnavi";
	public SharedPreferenceUtil(Context context) {
		if(context == null){
			Log.d("navi", "sharePreference ERRRRRRRRRRRRRRRRRRRRRRRR");
			return;
		}
		sp = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
		editor = sp.edit();
	 }

	public SharedPreferences.Editor getEditor(){
		return editor;
	}
	public SharedPreferences getSp(){
		return sp;
	}

	public void setServiceMCUVersion(int version) {
		editor.putInt("servicemcuversion", version);
		editor.commit();
	}

	public int getServiceMCUVersion() {
		return sp.getInt("servicemcuversion", 0);
	}

	public void setPasswd(String passwd) {
		editor.putString("passwd", passwd);
		editor.commit();
	}

	public String getPasswd() {
		return sp.getString("passwd", "");
	}

	public void setId(long id) {
		editor.putLong("id", id);
		editor.commit();
	}

	public long getId() {
		return sp.getLong("id", 0);
	}
	
	public void setCityName(String cityName) {
		editor.putString("CityName", cityName);
		editor.commit();
	}

	public String getCityName() {
		return sp.getString("CityName", "");
	}
	
	public void setDayNightMode(int id) {
		editor.putInt("DayNightMode", id);
		editor.commit();
	}

	public int getDayNightMode() {
		return sp.getInt("DayNightMode", 0);
	}
	
	public boolean getTrafficVoice() {
		return sp.getBoolean("TrafficVoice", true);
	}
	
	public void setTrafficVoice(boolean isTrafficVoiceOn) {
		editor.putBoolean("TrafficVoice", isTrafficVoiceOn);
		editor.commit();
	}
	
	public boolean getKeepScreenOn() {
		return sp.getBoolean("KeepScreenOn", false);
	}
	
	public void setKeepScreenOn(boolean isKeepScreenOn) {
		editor.putBoolean("KeepScreenOn", isKeepScreenOn);
		editor.commit();
	}
	
	public boolean getAvoidJam() {
		return sp.getBoolean("AvoidJam", false);
	}
	
	public void setAvoidJam(boolean isAvoidJam) {
		editor.putBoolean("AvoidJam", isAvoidJam);
		editor.commit();
	}
	
	public boolean getSaveMoney() {
		return sp.getBoolean("SaveMoney", false);
	}
	
	public void setSaveMoney(boolean isSaveMoney) {
		editor.putBoolean("SaveMoney", isSaveMoney);
		editor.commit();
	}
 
	public boolean getNoExpressWay() {
		return sp.getBoolean("NoExpressWay", false);
	}
	
	public void setNoExpressWay(boolean isNoExpressWay) {
		editor.putBoolean("NoExpressWay", isNoExpressWay);
		editor.commit();
	}
	
	public boolean getHighWayFirst() {
		return sp.getBoolean("HighWayFirst", false);
	}
	
	public void setHighWayFirst(boolean isHighWayFirst) {
		editor.putBoolean("HighWayFirst", isHighWayFirst);
		editor.commit();
	}
	
	
	public void setHistoryPOIs(List<ShortPOI> shortPOIs){
		if(shortPOIs == null)  return;
		JSONArray mJsonArray = new JSONArray();
		for(int i=0;i<shortPOIs.size();i++){
			ShortPOI poi = shortPOIs.get(i);
			if(poi == null) continue;
			JSONObject object = new JSONObject();
			try{
				object.put("id", poi.id);
			    object.put("title", poi.title);
			    object.put("address", poi.address);
			    object.put("latitude", poi.latitude);
			    object.put("longitude", poi.longitude);
			}catch(JSONException e){
				e.printStackTrace();
			}
			mJsonArray.put(object);
		}
		
		String keyName = "HistoryPOIs";
		String value = mJsonArray.toString();
		editor.putString(keyName, value);
		editor.commit();
	}
	
	
	public List<ShortPOI> getHistoryPOIs(){
		 List<ShortPOI> lstPOIs = new ArrayList<ShortPOI>();
		 String value = sp.getString("HistoryPOIs", null);
		 Log.d("navi", "getHistoryPOIs:"+value);
		 if(value == null) return null;
		 try{
			 JSONArray array = new JSONArray(value);
			 for(int i=0;i<array.length();i++){
				 JSONObject itemObject = array.getJSONObject(i);
				 ShortPOI poi = new ShortPOI();
//			     int id = itemObject.getInt("id");
			     poi.id = itemObject.getString("id");
			     poi.title = itemObject.getString("title");
			     poi.address = itemObject.getString("address");
			     poi.latitude = itemObject.getDouble("latitude");
			     poi.longitude = itemObject.getDouble("longitude");
			     lstPOIs.add(poi);
			 }
		 }catch(JSONException e){
			 e.printStackTrace();
			 return null;
		 }
		 return lstPOIs;
	}
	
	public void setLinesRecords(List<RouteLine> lstRouteLine){
		if(lstRouteLine == null) return;
		JSONArray mJsonArray = new JSONArray();
		for(int i=0;i<lstRouteLine.size();i++){
			RouteLine line = lstRouteLine.get(i);
			if(line == null) continue;
			JSONObject object = new JSONObject();
			try{
				if(line.startPoint.isCurrentPosition){
					object.put("s_cur", true);	
					object.put("s_title", "我的位置");
				}else {
					object.put("s_cur", false);
					object.put("s_id", line.startPoint.id);
				    object.put("s_title", line.startPoint.title);
//				    object.put("start_address", line.startPoint.address);
				    object.put("s_lat", line.startPoint.latitude);
				    object.put("s_lon", line.startPoint.longitude);
				}
				
				if(line.endPoint.isCurrentPosition){
					object.put("e_cur", true);	
					object.put("e_title", "我的位置");
				}else{
					object.put("e_cur", false);
					object.put("e_id", line.endPoint.id);
				    object.put("e_title", line.endPoint.title);
//				    object.put("start_address", line.startPoint.address);
				    object.put("e_lat", line.endPoint.latitude);
				    object.put("e_lon", line.endPoint.longitude);
				}
			    
			}catch(JSONException e){
				e.printStackTrace();
			}
			mJsonArray.put(object);
		}
		
		String keyName = "HistoryLines";
		String value = mJsonArray.toString();
		editor.putString(keyName, value);
		editor.commit();
	}
	
	
	public List<RouteLine> getLinesRecords(){
		 List<RouteLine> lstPOIs = new ArrayList<RouteLine>();
		 String value = sp.getString("HistoryLines", null);
		 Log.d("navi", "getLinesRecords:"+value);
		 if(value == null) return null;
		 try{
			 JSONArray array = new JSONArray(value);
			 for(int i=0;i<array.length();i++){
				 JSONObject itemObject = array.getJSONObject(i);
				 RouteLine line = new RouteLine();
				 line.startPoint = new ShortPOI();
				 line.endPoint = new ShortPOI();
				 line.startPoint.isCurrentPosition = itemObject.getBoolean("s_cur");
				 line.startPoint.title = itemObject.getString("s_title");
				 if(!line.startPoint.isCurrentPosition){
					 line.startPoint.id = itemObject.getString("s_id");
					 line.startPoint.latitude = itemObject.getDouble("s_lat");
					 line.endPoint.longitude = itemObject.getDouble("s_lon");
				 }
				 line.endPoint.isCurrentPosition = itemObject.getBoolean("e_cur");
				 line.endPoint.title = itemObject.getString("e_title");
				 if(!line.endPoint.isCurrentPosition){
					 line.endPoint.id = itemObject.getString("e_id");
					 line.endPoint.latitude = itemObject.getDouble("e_lat");
					 line.endPoint.longitude = itemObject.getDouble("e_lon");
				 }
			     lstPOIs.add(line);
			 }
		 }catch(JSONException e){
			 e.printStackTrace();
			 return null;
		 }
		 return lstPOIs;
	}
	
	
	
	
	
	
	public void setAppInfoList(List<AppInfo> appInfos){
		if(appInfos == null)  return;
		JSONArray mJsonArray = new JSONArray();
		for(int i=0;i<appInfos.size();i++){
			AppInfo appInfo = appInfos.get(i);
			if(appInfo == null) continue;
			JSONObject object = new JSONObject();
			try{
				object.put("id", i);//appInfos.keyAt(i));
			    object.put("package", appInfo.packageName);
			}catch(JSONException e){
				e.printStackTrace();
			}
			mJsonArray.put(object);
		}
		
		String keyName = "AppInfos";
		String value = mJsonArray.toString();
		Log.d("navi", "AppInfos set:"+value);
		editor.putString(keyName, value);
		editor.commit();
	}
	
	
	
	public List<AppInfo> getAppInfoList(){
		 String value = sp.getString("AppInfos", null);
		 if(value == null) return null;
		 Log.d("navi", "AppInfos get:"+value);
		 List<AppInfo> appInfos = null;
		 try{
			 JSONArray array = new JSONArray(value);
			 for(int i=0;i<array.length();i++){
				 JSONObject itemObject = array.getJSONObject(i);
				 if(appInfos == null)  appInfos = new ArrayList<AppInfo>();
			     AppInfo appInfo = new AppInfo();
			     int id = itemObject.getInt("id"); 
			     appInfo.packageName = itemObject.getString("package");
//			     appInfos.put(0, appInfo);
			     appInfos.add(appInfo);
			 }
		 }catch(JSONException e){
			 e.printStackTrace();
			 return null;
		 }
		 return appInfos;
	}

	
	public void setHomeAddress(ShortPOI poi) {
		// TODO Auto-generated method stub
		if(poi == null)
			return;
		String keyName = "HomeAddress";
		JSONObject object = new JSONObject();
		try{
			object.put("id", poi.id);
		    object.put("title", poi.title);
		    object.put("address", poi.address);
		    object.put("latitude", poi.latitude);
		    object.put("longitude", poi.longitude);
		}catch(JSONException e){
			e.printStackTrace();
		}
		String value = object.toString();//poi.id+";"+poi.title+";"+poi.address+";"+(int)(poi.latitude*100000)+";"+(int)(poi.longitude*100000);
		editor.putString(keyName, value);
		editor.commit();
	}

	public void setOfficeAddress(ShortPOI poi) {
		// TODO Auto-generated method stub
		if(poi == null)
			return;
		String keyName = "OfficeAddress";
		JSONObject object = new JSONObject();
		try{
			object.put("id", poi.id);
		    object.put("title", poi.title);
		    object.put("address", poi.address);
		    object.put("latitude", poi.latitude);
		    object.put("longitude", poi.longitude);
		}catch(JSONException e){
			e.printStackTrace();
		}
		String value = object.toString();//poi.id+";"+poi.title+";"+poi.address+";"+(int)(poi.latitude*100000)+";"+(int)(poi.longitude*100000);
		editor.putString(keyName, value);
		editor.commit();
	}


	public ShortPOI getHomeAddress() {
		// TODO Auto-generated method stub
		String keyName = "HomeAddress";
		String value = sp.getString(keyName, "");
		ShortPOI poi = null;
		if(value.equals(""))
			return poi;
		try {
			JSONObject itemObject = new JSONObject(value);
			 poi = new ShortPOI();
			 poi.id = itemObject.getString("id");
		     poi.title = itemObject.getString("title");
		     poi.address = itemObject.getString("address");
		     poi.latitude = itemObject.getDouble("latitude");
		     poi.longitude = itemObject.getDouble("longitude");
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			poi = null;
			e1.printStackTrace();
		}
		return poi;
	}

	public ShortPOI getOfficeAddress() {
		// TODO Auto-generated method stub
		String keyName = "OfficeAddress";
		String value = sp.getString(keyName, "");
		ShortPOI poi = null;
		if(value.equals(""))
			return poi;
		try {
			JSONObject itemObject = new JSONObject(value);
			 poi = new ShortPOI();
			 poi.id = itemObject.getString("id");
		     poi.title = itemObject.getString("title");
		     poi.address = itemObject.getString("address");
		     poi.latitude = itemObject.getDouble("latitude");
		     poi.longitude = itemObject.getDouble("longitude");
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			poi = null;
			e1.printStackTrace();
		}
		return poi;
	}
	
	
}
